#ifndef __FPGA_GLOBAL_MEM_H__
#define __FPGA_GLOBAL_MEM_H__

#include "graph_fpga.h"

#include "fpga_application.h"

template <typename T>
void burstRead2in(
    uint_raw                        edge_offset,
    uint_raw                        edge_end,
    uint16                          *input1,
	uint16                          *input2,
    hls::stream<T>                  &outputstream
)
{
#pragma HLS function_instantiate variable=input1
#pragma HLS function_instantiate variable=outputstream
    unsigned int offset = 0;
    unsigned int end = ((edge_end - 1) >> (LOG2_SIZE_BY_INT+1)) + 1;

    {

timeLineLoop : for (unsigned int i = (offset); i < (end); i +=1)
        {
#pragma HLS PIPELINE II=1
			burst_token token;

	        token.data.range(511,0) = input1[i];
	        token.data.range(1023,512) = input2[i];
	        token.flag = FLAG_RESET;
	        write_to_stream(outputstream, token);
        }
    }

    {
        burst_token token;
        token.flag = FLAG_SET;
        write_to_stream(outputstream, token);
        return;
    }

    return;
}

template <typename T>
void burstRead(
    uint_raw                        edge_offset,
    uint_raw                        edge_end,
    uint16                          *input,
    hls::stream<T>                  &outputstream
)
{
#pragma HLS function_instantiate variable=input
#pragma HLS function_instantiate variable=outputstream
    unsigned int offset = 0;
    unsigned int end = ((edge_end - 1) >> (LOG2_SIZE_BY_INT)) + 1;

    {

timeLineLoop : for (unsigned int i = (offset); i < (end); i +=2)
        {
#pragma HLS PIPELINE II=1
			burst_token token;

	        token.data.range(511,0) = input[i];
	        token.data.range(1023,512) = input[i+1];
	        token.flag = FLAG_RESET;
	        write_to_stream(outputstream, token);
        }
    }

    {
        burst_token token;
        token.flag = FLAG_SET;
        write_to_stream(outputstream, token);
        return;
    }

    return;
}




void writeBack(int idx, uint16 *addr, hls::stream<burst_raw>  &input)
{
#pragma HLS function_instantiate variable=idx

    unsigned int counter = 0;
    for (int i = 0; i < 0x7fffffff; i++)
    {
#pragma HLS PIPELINE II=1
    	burst_raw tmp;
    	read_from_stream(input, tmp);
    	if (tmp.range(BURST_ALL_BITS - 1, BURST_ALL_BITS - 32) == ENDFLAG)
    	{
    		return;
    	}
        addr[i] = tmp;
    }
}



#endif /*__FPGA_GLOBAL_MEM_H__ */
